<?php
namespace Admin\Controller;
use Think\Controller;
class CustomerController extends Controller {
	/**
	 * 	客户管理列表
	 */
	public function index() {
		$meid = $this->Islogin();
		$customer_db = D('Customer');
		$field = [
			'jdc_user.id',
			'jdc_user.nickname',
			'jdc_user.tel',
			'jdc_user.waynum',
			'pay.orderid'
		];
		$limit = 6;
		$page = I('get.page');
		if($page>0) {
			// $jlr = I('get.page');
			$limit2 = intval($page);
			$lr = [intval($page)*$limit,$limit];
		}else{
			$lr = [0,$limit];
			// $limit2 = $limit;
		}
		$customer_data = $customer_db->AllCustomerList($field,$lr,$meid);
		if($customer_data){
			echo json_encode(['success'=>'OK','data'=>$customer_data,'page'=>$limit2]);return;
		}else{
			echo json_encode(['error'=>'1','message'=>'没有更多数据！']);return;
		}
	}


	/**
	 * 客户关联渠道
	 */
	public function BindChannel() {
		$customer_db = D('Customer');
		$channel_db = M('wayuser');
		$post = I('post.');
		// 参数必须持有渠道码
		if(empty($post['waynum'])){
			echo json_encode(['error'=>'3','message'=>'缺少渠道推荐码！']);	
			return;
		}
		$post['time'] = time();
		$code = $post['code'];
		$phone = $post['phone'];
		$secode = session('customer_code');
		$secode_time = session('customer_codetime');
		$timeout = $post['time'] - $secode_time;
		// 对比客户验证码
		if($secode!=$code){
			echo json_encode(['error'=>'1','message'=>'验证码错误！']);
			return;
		}
		if($timeout>=300){
            echo json_encode(['error'=>'1','message'=>'验证码已过期，请重新输入验证码！']);return;
		}
		
        // 查询渠道数据
        $channelres = $channel_db->where('waynum='.$post['waynum'].' and status=2')->find();
        if(!$channelres){
        	echo json_encode(['error'=>'3','message'=>'该推荐码不存在或为未激活状态，请确认！']);
			return;
        }
        // 获取用户信息
		$resmsg = $customer_db->OnefieldData(['tel'=>$phone]);
		if($resmsg){
			if(!empty($resmsg['waynum'])&&$resmsg['waynum']!=0){ 
				echo json_encode(['error'=>'3','message'=>'你已经绑定过渠道了！']);
				return;
			}
			// 更新客户信息
			$res = $customer_db->Update(['tel'=>$phone],['waynum'=>$post['waynum']]);
		}else{
			// 新建客户信息
			$res = $customer_db->UserInsert($post);
		}
		
		if($res){
			//个人渠道人数统计
			$channelNum = $channel_db->where('waynum='.$post['waynum'])->find();
			$channelNum['count'] = $channelNum['count']+1;
			$channel_db->where('waynum='.$post['waynum'])->save($channelNum);
			echo json_encode(['success'=>'OK','message'=>'完成！']);
			return;
		}else{
			echo json_encode(['error'=>'2','message'=>'系统错误，请联系客服处理！']);
			return;
		}
	}
	
	/**
     * 发送验证码
     * @return [type] [description]
     */
    public  function  SendCode() {
        $mode = M('admin');
        $mobilephone = I('post.phone');
        // 限制手机格式
        if(!preg_match("/1[34578]{1}\d{9}$/",$mobilephone)){
            echo json_encode(['error'=>'1','message'=>'请输入正确的手机号！']);return;
        }
        $time = time();
        $codetimeout = session('customer_codetime');
        // 1分钟内不能重复操作
        if(!empty($codetimeout)){
            $timeout = $time-$codetimeout;
            if($timeout<=60){
                 echo json_encode(['error'=>'2','message'=>'请勿频繁操作！']);return;
            }
        }
        // 随机验证码
        $checkcode = randomkeys(5,1);
        session('customer_code',$checkcode);
		session('customer_codetime',$time);
		$code = session('customer_code');
        $result = messagelog($mobilephone,$checkcode);
        echo json_encode(['success'=>'OK','message'=>$result]);
    }
    /**
     * 统计通过渠道二维码的访问量
     */
    public function addCountVisit(){
    	$data  = I('post.');
    	$wayum = M('wayuser');
    	$res = $wayum->where(array('waynum'=>$data['waynum']))->setInc('countvisit',1);
    	if($res){
    		$this->ajaxReturn(array('ret'=>"OK"));	
    	}
    }
}